"""
Description:
Version: 2.0
Autor: zyw
Date: 2022-01-11 14:08:04
LastEditors: zyw
LastEditTime: 2022-01-11 15:02:12
"""
import pytest
import requests
import os, sys
from jsonpath import jsonpath

base_path = os.path.abspath(os.path.join(os.getcwd(), "."))
sys.path.append(base_path)
from common.hand_conf import conf


# 用例的前置方法，提取token
@pytest.fixture()
def cls_setup():
    """用力类的前置方法：提取token"""
    # 1.请求登陆接口，进行登录
    url = conf.get('env', 'base_url') + '/auth/login'
    params = {
        'username': conf.get('test_data', 'username'),
        'password': conf.get('test_data', 'password'),
        'picCode': conf.get('test_data', 'picCode')
    }
    headers = eval(conf.get('header', 'login_header'))
    response = requests.post(url, data=params, headers=headers)
    res = response.json()
    # 2.登录成功后提取token出来
    token = jsonpath(res, '$..token')[0]
    # 将token添加到请求头中
    headers["token"] = token
    yield headers
